﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BO = Viewer.BusinessObject.BusinessEntities;
using Ext.Net;
using System.Collections.Generic;
using Viewer.BusinessObject.BusinessEntities;
namespace Viewer.ToChuc
{
    public partial class PhanQuyenTheoToChuc : System.Web.UI.Page
    {
        BO.CommonCode common = new Viewer.BusinessObject.BusinessEntities.CommonCode();
        BO.NSToChuc query = new Viewer.BusinessObject.BusinessEntities.NSToChuc();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                LoadDataForTree();
                LoadDataControl();
            }

        }
        protected void btnSearch_Click(object sender, DirectEventArgs e)
        {
            int idNode = !string.IsNullOrEmpty(hdNodeId.Text) ? Convert.ToInt16(hdNodeId.Text) : 0;
            int tochucChaId = !string.IsNullOrEmpty(cbbTochucCha.SelectedItem.Value) ? Convert.ToInt16(cbbTochucCha.SelectedItem.Value) : 0;
            int khoiId = !string.IsNullOrEmpty(cbbKhoi.SelectedItem.Value) ? Convert.ToInt16(cbbKhoi.SelectedItem.Value) : 0;
            int xephangId = !string.IsNullOrEmpty(cbbXepHang.SelectedItem.Value) ? Convert.ToInt16(cbbXepHang.SelectedItem.Value) : 0;
            storeTochuc.DataSource = query.SearchCondition(idNode, txtTenToChuc.Text, khoiId, xephangId, tochucChaId);
            storeTochuc.DataBind();
        }
        void LoadDataControl()
        {
            storeXepHang.DataSource = common.LoadDataCommonByCodeKey("XH");
            storeXepHang.DataBind();
            storeKhoi.DataSource = common.LoadDataCommonByCodeKey("Khoi");
            storeKhoi.DataBind();
            storeToChucCha.DataSource = query.LoadAllToChuc();
            storeToChucCha.DataBind();
        }

        /// <summary>
        /// Loads the data for tree.
        /// </summary>
        /// <author>baodt</author>
        /// <datetime>7/1/2011-10:39 AM</datetime>
        [DirectMethod]
        public void LoadDataForTree()
        {
            //Load Root                     
            Ext.Net.TreeNode root = new Ext.Net.TreeNode("Tổ Chức Hành Chính");
            root.Expanded = true;
            root.CustomAttributes.Add(new ConfigItem("IsView", "0"));//disable
            if (HttpContext.Current.User.Identity.Name.ToLower() != "admin")
            {
                root.Disabled = true;
            }
            TreePanel.Root.Add(root);
            List<NSToChuc> lstToChuc = query.GetAll();
            List<NSToChuc> lstToChucLv1 = lstToChuc.Where(c => c.ToChucLevel == 1).ToList();

            foreach (var oToChucLv1 in lstToChucLv1)
            {
                var nodeLv1 = CreateNode(oToChucLv1);
                root.Nodes.Add(nodeLv1);
                List<NSToChuc> lstToChucLv2 = lstToChuc.Where(c => c.ToChucIDCha == oToChucLv1.ToChucID).ToList();
                foreach (var oToChucLv2 in lstToChucLv2)
                {
                    var nodeLv2 = CreateNode(oToChucLv2);
                    nodeLv1.Nodes.Add(nodeLv2);
                    List<NSToChuc> lstToChucLv3 = lstToChuc.Where(c => c.ToChucIDCha == oToChucLv2.ToChucID).ToList();
                    foreach (var oToChucLv3 in lstToChucLv3)
                    {
                        var nodeLv3 = CreateNode(oToChucLv3);
                        nodeLv2.Nodes.Add(nodeLv3);
                    }
                }
            }
            if (X.IsAjaxRequest)
            {
                Ext.Net.X.AddScript("refreshTree({0},{1});", TreePanel.ClientID, TreePanel.Root.ToJson());
            }
        }
        private Ext.Net.TreeNode CreateEnableNode(NSToChuc oToChuc, bool IsView, bool IsAdd, bool IsEdit, bool IsDelete)
        {
            Ext.Net.TreeNode node = new Ext.Net.TreeNode(oToChuc.ToChucID.ToString(), oToChuc.TenToChucFull, Icon.Group);
            node.CustomAttributes.Add(new ConfigItem("IsView", IsView ? "1" : "0"));
            node.CustomAttributes.Add(new ConfigItem("IsAdd", IsAdd ? "1" : "0"));
            node.CustomAttributes.Add(new ConfigItem("IsEdit", IsEdit ? "1" : "0"));
            node.CustomAttributes.Add(new ConfigItem("IsDelete", IsDelete ? "1" : "0"));

            node.Expanded = true;
            return node;
        }
        private Ext.Net.TreeNode CreateNode(NSToChuc oToChuc)
        {
            //Hard code for admin 
            if (Context.User.Identity.Name.ToLower() == "admin")
            {
                return CreateEnableNode(oToChuc, true, true, true, true);
            }
            bool IsView = false;
            bool IsAdd = false;
            bool IsEdit = false;
            bool IsDelete = false;

            //1 nguoi thuoc nhieu nhom quyen
            if (this.NhomQuyenByToChuc_ToChuc == null)
            {
                return CreateDisableNode(oToChuc.ToChucID.ToString(), oToChuc.TenToChucFull);
            }
            var lstNhomQuyenByToChuc_ToChuc = (from m in this.NhomQuyenByToChuc_ToChuc
                                               where m.ToChucID == oToChuc.ToChucID
                                               select m).ToList();
            if (lstNhomQuyenByToChuc_ToChuc.Count == 0)
            {
                //khong co quyen doi voi to chuc nay
                return CreateDisableNode(oToChuc.ToChucID.ToString(), oToChuc.TenToChucFull);
            }

            foreach (var item in lstNhomQuyenByToChuc_ToChuc)
            {
                IsView = IsView || item.IsView;
                IsAdd = IsAdd || item.IsAdd;
                IsEdit = IsEdit || item.IsEdit;
                IsDelete = IsDelete || item.IsDelete;
            }

            /////////////////
            if (!IsView)
            {
                return CreateDisableNode(oToChuc.ToChucID.ToString(), oToChuc.TenToChucFull);
            }
            return CreateEnableNode(oToChuc, IsView, IsAdd, IsEdit, IsDelete);
        }
        private Ext.Net.TreeNode CreateDisableNode(string id, string text)
        {
            Ext.Net.TreeNode node = new Ext.Net.TreeNode(id + "Decline", text, Icon.GroupKey);
            node.CustomAttributes.Add(new ConfigItem("IsView", "0"));
            node.Disabled = true;
            node.Cls = "Disable";
            node.Expanded = true;
            return node;
        }
        private List<QUNhomQuyenByToChuc_ToChuc> _nhomquyen;
        public List<QUNhomQuyenByToChuc_ToChuc> NhomQuyenByToChuc_ToChuc
        {
            get
            {
                if (_nhomquyen == null)
                {
                    //var oNguoidung = QUNguoiDung.GetByUserName(Page.User.Identity.Name.ToString());
                    //_nhomquyen = oNguoidung.NhomQuyenByToChuc_ToChuc();
                    _nhomquyen = (List<QUNhomQuyenByToChuc_ToChuc>)Session["NhomQuyenByToChuc_ToChuc"];
                }
                return _nhomquyen;
            }
        }

        [DirectMethod]
        public void LoadGrid(string ID, string nodeText)
        {
            int id;
            try
            {
                id = int.Parse(ID);
            }
            catch (Exception)
            {

                id = 0;
            }
            hdNodeName.Value = nodeText;
            hdNodeId.Text = id.ToString();
            hdIdCha.Value = id;
            storeTochuc.DataSource = query.LoadDataForGrid(id);
            storeTochuc.DataBind();
            grdToChuc.DataBind();

        }
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            int idCha = hdIdCha.Value.ToString() != string.Empty ? Convert.ToInt16(hdIdCha.Value) : 0;
            UcAddToChuc.ShowAddNewDialog("AddNew", 0, idCha);
        }
        [DirectMethod]
        public void ShowDialog(string type, int ID)
        {

            if (type.Equals("Change"))
            {
                UCChangeToChuc.ShowDialogChange(ID, "Tochuc", hdNodeName.Value.ToString());
            }
            else
            {
                int idCha = hdIdCha.Value.ToString() != string.Empty ? Convert.ToInt16(hdIdCha.Value) : 0;
                UcAddToChuc.ShowAddNewDialog(type, ID, idCha);
            }
        }

        [DirectMethod]
        public void DeleteTochuc(int id)
        {
            int idCha = hdIdCha.Value.ToString() != string.Empty ? Convert.ToInt16(hdIdCha.Value) : 0;
            BO.NSToChuc query = new Viewer.BusinessObject.BusinessEntities.NSToChuc();
            query.DeleteToChuc(id);
            LoadGrid(idCha.ToString(),"");
            LoadDataForTree();
        }
    }
}
